const { getProjectNameLang } = require("../common");

// packageH/project_verification/BusinessVerification/BusinessVerification.js
const App = getApp();
const GetProjectUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.get-service-info");
const PostCountUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.create");
const GetProjectDetailsUrl = App.getNetAddresss("plugin.store-projects.frontend.project-service.get-detail");
Page({
  /**
   * 页面的初始数据
   */
  data: {
    projectOrderId: null,
    project: null,
    used_count: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let projectId = null;
    if (options.scene) {
      let scene = this.sceneDecode(options.scene);
      projectId=scene.service_id;
    } else if (options.id) {
      projectId = options.id;
    }
    if (!projectId) {
      wx.showToast({
        title: getProjectNameLang() + "不存在",
        icon: "none",
      });
      wx.navigateBack({
        delta: 1,
      });
      return;
    }
    this.setData({
      projectOrderId: projectId,
    });
    this.getProject();
  },
  sceneDecode(scene) {
    scene = decodeURIComponent(scene);
    let paramArr = scene.split("&");
    const senceValues = {};

    paramArr.forEach((item, index) => {
      let key = item.split("=")[0];
      let value = item.split("=")[1];
      senceValues[key] = value;
    });
    return senceValues;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () { },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () { },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () { },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () { },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.getProject();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () { },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () { },
  inputElChanged({ detail }) {
    this.setData({
      used_count: detail.value,
    });
  },
  getProject() {
    wx.showLoading();
    App._getNetWork({
      url: GetProjectUrl,
      data: {
        id: this.data.projectOrderId,
      },
      success: ({ data: { data: response, result, msg } }) => {
        wx.hideLoading();
        wx.stopPullDownRefresh();
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        this.setData({
          project: response,
        });
      },
    });
  },
  confirmWriteOff() {
    if (this.data.project.rest_count <= 0) {
      wx.showToast({
        title: "剩余次数不足，请核对后核销",
        icon: "none",
      });
      return;
    }
    if (!this.data.used_count) {
      wx.showToast({
        title: "请输入正确的核销次数",
        icon: "none",
      });
      return;
    }
    wx.showLoading();
    App._getNetWork({
      url: PostCountUrl,
      data: {
        id: this.data.projectOrderId,
        used_count: this.data.used_count,
      },
      success: ({ data: { data: response, result, msg } }) => {
        wx.hideLoading();
        if (result === 0) {
          wx.showToast({
            title: msg,
            icon: "none",
          });
          return;
        }
        wx.showToast({
          title: "核销成功",
          icon: "none",
        });
        App._getNetWork({
          url: GetProjectDetailsUrl,
          data: {
            id: response.id,
          },
          success: ({ data: { data: response, result, msg } }) => {
            if (result === 0) {
              wx.showToast({
                title: msg,
                icon: "none",
              });
              return;
            }
            this.setData({
              ["project.rest_count"]: response.total_rest_count,
              ["project.used_count"]: response.total_use_count,
              ["project.end_time"]: response.end_time,
            });
          },
        });
      },
    });
  },
});
